package com.j48.entrancegurad.controller;

import com.alibaba.fastjson.JSONArray;
import com.j48.entrancegurad.model.A_C;
import com.j48.entrancegurad.model.Character;
import com.j48.entrancegurad.model.P_C;
import com.j48.entrancegurad.model.Power;
import com.j48.entrancegurad.service.IA_CService;
import com.j48.entrancegurad.service.ICharacterService;
import com.j48.entrancegurad.service.IP_CService;
import com.j48.entrancegurad.service.IPowerService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CharacterController {
    @Autowired
    private ICharacterService service;
    @Autowired
    private IA_CService a_cservice;
    @Autowired
    private IP_CService p_cservice;
    @Autowired
    private ICharacterService characterService;
    @Autowired
    private IPowerService powerService;

    /**
     * 修改角色
     * @return 页面地址
     */
    @RequestMapping("/modifyroles1")
@ResponseBody
public String selectcharacterall(HttpServletRequest req,Character character,P_C p_c,Power power){
        int roleid=Integer.parseInt(req.getParameter("cid"));
        System.out.println(roleid);
        Character character1=new Character();
        character1.setCharacter_id(roleid);
        String rolename=characterService.selectById(character1).getCharacter_name();
        character.setCharacter_name(rolename);
        ArrayList<Character> characterServicelist = service.selectcharacter(character);
        p_c.setP_c_character_id(characterServicelist.get(0).getCharacter_id());
        ArrayList<P_C> powerids= p_cservice.selectall(p_c);
        ArrayList<Integer> power_id=new ArrayList<Integer>();
        for(int i=0;i<powerids.size();i++){
           int x= powerids.get(i).getP_c_power_id();
           power_id.add(i,x);
        }
        ArrayList<String> powernames=new ArrayList<String>();
        for(int i=0;i<power_id.size();i++){
            power.setPower_id(power_id.get(i));;
            powernames.add(i,powerService.selectByID(power).get(0).getPower_name());
        }
        req.getSession().setAttribute("rolename",rolename);
        req.getSession().setAttribute("powernameall",powernames);
        return "true";
}

    /**
     * 删除角色
     * @param req
     * @return
     */
    @RequestMapping("/deleterole")
    @ResponseBody
    public String deletecharacter(HttpServletRequest req, Character character, A_C a_c, P_C p_c){
        Character character1=new Character();
        int roleid= Integer.parseInt(req.getParameter("cid1"));
        character1.setCharacter_id(roleid);
        String rolename=characterService.selectById(character1).getCharacter_name();
        System.out.println(roleid+"--"+rolename);
        character.setCharacter_name(rolename);
        character.setCharacter_id(roleid);
        a_c=new A_C();
        a_c.setA_c_character_id(roleid);
        ArrayList<A_C> a_c1=new ArrayList<A_C>();
        a_c1=a_cservice.selectroleid(a_c);
        p_c.setP_c_character_id(roleid);
        System.out.println(a_c1);
        if(a_c1.size()==0){
            boolean juge=p_cservice.deleteBycharacterid(p_c);
            if(juge){
                service.deletecharacter(character);
            return "true";
            }else{
            service.deletecharacter(character);
            return "true";
            }
        }
        return "false";
    }

    /**
     * 摧毁session
     * @param
     * @return
     */
    @RequestMapping("/modifyroleflush")
    @ResponseBody
    public ModelAndView modifyroleflush1(ModelAndView model){
        model=new ModelAndView("jsp/modifyrole");
        return model;
    }

    /**
     * 摧毁session
     * @param req
     * @return
     */
    @RequestMapping("/flush")
    @ResponseBody
    public String flushpage(HttpServletRequest req){
        req.getSession().removeAttribute("rolelist");
        ArrayList<Character> list = characterService.selectcharacterall();
        req.getSession().setAttribute("rolelist",list);
        return "true";
    }

    /**
     * 摧毁session
     * @param req
     * @return
     */
    @RequestMapping("/flush1")
    @ResponseBody
    public ModelAndView flushpage1(HttpServletRequest req,ModelAndView model){
        req.getSession().removeAttribute("rolelist");
        ArrayList<Character> list = characterService.selectcharacterall();
        req.getSession().setAttribute("rolelist",list);
        model=new ModelAndView("jsp/addrole");
        return model;
    }

    /**
     * 摧毁session
     * @param req
     * @return
     */
    @RequestMapping("/flush2")
    @ResponseBody
    public ModelAndView flushpage2(HttpServletRequest req,ModelAndView model){
        req.getSession().removeAttribute("rolelist");
        ArrayList<Character> list = characterService.selectcharacterall();
        req.getSession().setAttribute("rolelist",list);
        model=new ModelAndView("jsp/roleManagement");
        return model;
    }

    /**
     * 按名称查询角色
     * @param req
     * @return
     */
    @RequestMapping("/selectcharacter")
    @ResponseBody
    public ModelAndView selectcharacter(HttpServletRequest req,Character character,ModelAndView model){
        String rolename=req.getParameter("rolename");
        character.setCharacter_name(rolename);
        ArrayList<Character> character1=new ArrayList<Character>();
        if(!rolename.equals("")){
            character1=characterService.selectcharacter(character);
            if(character1.size()>0){
        model=new ModelAndView("jsp/roleManagement");
        req.getSession().setAttribute("rolelist",character1);
        return model;
            }
            String s="";
            model=new ModelAndView("jsp/roleManagement");
            req.getSession().setAttribute("rolelist",s);
            return model;
        }
        ArrayList<Character> list = characterService.selectcharacterall();
        model=new ModelAndView("jsp/roleManagement");
        req.getSession().setAttribute("rolelist",list);
        return model;
    }

    /**
     * 添加角色
     * @param power 权限
     * @param character 角色
     * @param p_c 角色权限对应关系
     * @param req
     * @return
     */
    @RequestMapping("/addcharacter")
    @ResponseBody
    public String addrole(Power power,Character character,P_C p_c,HttpServletRequest req){
        service.addcharacter(character);//添加角色名称
        ArrayList<Character> character1=service.selectcharacter(character);
        int characterid=character1.get(0).getCharacter_id();
        String powerName[]=req.getParameterValues("power_name");
        if(powerName==null){
            return "false";
        }
        ArrayList<Integer> powerlist=new ArrayList<Integer>();
        for(int i=0;i<powerName.length;i++){
              power.setPower_name(powerName[i]);
              Power p=powerService.selectpower(power);
              int powerid = p.getPower_id();
              powerlist.add(i,powerid);
        }
        for(int i=0;i<powerlist.size();i++){
            p_c.setP_c_character_id(characterid);
            p_c.setP_c_power_id(powerlist.get(i));
            p_c.setP_c_status(1);
            p_cservice.addP_c(p_c);
        }
        return "true";
    }

    /**
     * 修改角色
     * @param power 权限
     * @param character 角色
     * @param p_c 权限角色关系
     * @param req
     * @param p_c1 权限角色关系
     * @return
     */
    @RequestMapping("/modifycharacter")
    @ResponseBody
    public String modify(Power power,Character character,P_C p_c,HttpServletRequest req,P_C p_c1){
        String rolename=req.getParameter("character_name");
        String beforerolename = (String)req.getSession().getAttribute("rolename");
        req.getSession().setAttribute("rolename",rolename);
        character.setCharacter_name(beforerolename);
            ArrayList<Character> characterrole = service.selectcharacter(character);
            int characterid=characterrole.get(0).getCharacter_id();
            Character role=new Character();
            role.setCharacter_id(characterid);
            role.setCharacter_name(rolename);
            service.updaterolename(role);
        String powerName[]=req.getParameterValues("power_name");
        ArrayList<Character> character1=service.selectcharacter(role);
         int characterid1=character1.get(0).getCharacter_id();
        if(powerName==null){
            return "false";
        }
        ArrayList<Integer> powerlist=new ArrayList<Integer>();
        for(int i=0;i<powerName.length;i++){
            power.setPower_name(powerName[i]);
            Power p=powerService.selectpower(power);
            int powerid = p.getPower_id();
            powerlist.add(i,powerid);
        }
            p_c1.setP_c_character_id(characterid1);
            p_cservice.deletep_c(p_c1);
        for(int i=0;i<powerlist.size();i++){
            p_c.setP_c_character_id(characterid1);
            p_c.setP_c_power_id(powerlist.get(i));
            p_c.setP_c_status(1);
            p_cservice.addP_c(p_c);
        }
        return "true";
    }

    /**
     * 查询是否重复角色名
     * @param resp
     * @param character
     * @throws IOException
     */
    @RequestMapping("/jugerepeat")
    @ResponseBody
    public void selectrepeat(HttpServletResponse resp, Character character) throws IOException {
        PrintWriter w=resp.getWriter();
        ArrayList<Character> role = service.selectcharacter(character);
        ArrayList<Character> role1 = service.selectcharacter1(character);
        if(role.size()>0||role1.size()>0){
            w.write("false");
        }else{
            w.write("true");
        }
    }

    /**
     * 查询是否重复角色名
     * @param resp
     * @param character
     * @param req
     * @throws IOException
     */
    @RequestMapping("/jugerepeat1")
    @ResponseBody
    public void selectrepeat1(HttpServletResponse resp, Character character,HttpServletRequest req) throws IOException {
        PrintWriter w=resp.getWriter();
        String rolename=(String)req.getSession().getAttribute("rolename");
        ArrayList<Character> role = service.selectcharacter(character);
        if((role.size()==1&&role.get(0).getCharacter_name().equals(rolename))||role.size()==0){
            w.write("true");
        }else{
            w.write("false");
        }
    }
}
